ConsignModel = {};
local this = ConsignModel;

this.MenuList = {}
this.ConsignList = {}
this.NPC_X = 0
this.NPC_Y = 0

function ConsignModel.GetMenuData()
    return G.dataTable["AuctionMenu"]
end

function ConsignModel.GetMainMenuData()
    local tab = {}
    for k,v in pairs(this.GetMenuData()) do
        if v.PreID == 0 then
            table.insert(tab,v)
        end
    end
    return tab
end
function ConsignModel.GetChildMenuData()
    local tab = {}
    for k,v in pairs(this.GetMenuData()) do
        if v.PreID ~= 0 then
            table.insert(tab,v)
        end
    end
    return tab
end

function ConsignModel.GetNPC()
    local type = ConsignManager.GetMoneyType()
    if type == 0 then
        this.NPC_X = 5
        this.NPC_Y = 0
    else
        this.NPC_X = 6
        this.NPC_Y = 0
    end
end
--请求寄卖物品的菜单列表(绕过协议，直接读表)
function ConsignModel.SendItemMenu()
    SendMsg.Send(Config.msg_type.send_consignItemMenu,0);
    local name = getProtocol(Config.msg_main_type.main_consign,Config.sub_type_consign.sub_itemMenu);
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        if d ~= nil and d["menu"] ~= nil then
            this.MenuList = d["menu"]
        end
    end )
end
--寄卖包裹中某个物品 1.物品ID 2.寄卖数量 3.游戏币or金券(0 or 1) 4.价格
function ConsignModel.SendItemResult(itemId,count,moneyType,money)
    this.GetNPC()
    SendMsg.Send(Config.msg_type.send_consignItemResult,0,this.NPC_X,this.NPC_Y,itemId,count,moneyType,money);
    local name = getProtocol(Config.msg_main_type.main_consign,Config.sub_type_consign.sub_itemResult);
    ListenEvent(name,function (d)
        CloseEvent(name);
        --printTable(d)
        if d.opt ~= 0 then
            UISysTips.AddMsg(d.errorMsg)
        end
    end )
end
--购买寄卖的某个物品
function ConsignModel.SendItemPay(id)
    this.GetNPC()
    SendMsg.Send(Config.msg_type.send_consignItemPay,0,this.NPC_X,this.NPC_Y,id);
    local name = getProtocol(Config.msg_main_type.main_consign,Config.sub_type_consign.sub_itemPay);
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        ConsignManager.BuyCallBack(d)
    end )
end
--查看寄卖列表 父类型、子类型、名称、品质、起点、请求数
function ConsignModel.SendItemList(parent,child,name,quality,index,indexCount)
    this.GetNPC()
    SendMsg.Send(Config.msg_type.send_consignItemList,0,this.NPC_X,this.NPC_Y,parent or 0,child or 0,name or "",quality or 255,index or 0,indexCount or 255);
    local name = getProtocol(Config.msg_main_type.main_consign,Config.sub_type_consign.sub_itemList);
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        ConsignManager.OnItemList(d)
    end )
end
--查看个人的寄卖列表
function ConsignModel.SendItemListSelf()
    this.GetNPC()
    SendMsg.Send(Config.msg_type.send_consignItemListSelf,0,this.NPC_X,this.NPC_Y)
    local name = getProtocol(Config.msg_main_type.main_consign,Config.sub_type_consign.sub_itemListSelf)
    CloseEvent(name)
    ListenEvent(name,function (d)
        --printTable(d)
        ConsignManager.OnMyItemList(d)
    end )
end
--取消寄卖某个物品
function ConsignModel.SendItemCancel(id)
    this.GetNPC()
    SendMsg.Send(Config.msg_type.send_consignItemCancel,0,this.NPC_X,this.NPC_Y,id);
    local name = getProtocol(Config.msg_main_type.main_consign,Config.sub_type_consign.sub_itemCancel);
    CloseEvent(name);
    ListenEvent(name,function (d)
        --printTable(d)
        ConsignManager.CancelConsignCB(d)
    end )
end

return ConsignModel